package org.douxiaoban.client.activity;

import org.douxiaoban.client.R;
import org.douxiaoban.client.adapter.MailAdapter;
import org.douxiaoban.client.api.DoubanList.DoubanListType;
import org.douxiaoban.client.api.MailList;
import org.douxiaoban.client.app.Assistant;
import org.douxiaoban.client.util.WorkspaceView;
import org.douxiaoban.client.util.WorkspaceView.ScrollToScreenCallback;
import org.douxiaoban.client.widget.PopupRecommend;
import org.douxiaoban.client.widget.TitleStyle;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;

import com.mobclick.android.MobclickAgent;

public class MailActivity extends BaseActivity implements ScrollToScreenCallback{
	private WorkspaceView mailWorkspace;
	private MailList mInbox;
	private MailList mOutbox;

	
	private TitleStyle mTitle;

	
	private PopupRecommend mPopup;
	
	
	public void onCreate(Bundle bundle){
		super.onCreate(bundle);		
		//setContentView(R.layout.mail_home);
		mailWorkspace = new WorkspaceView(this, null);
	     requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
	     setContentView(mailWorkspace);
	     getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title);

        mTitle = new TitleStyle(this);
        mTitle.initTitleStyle();
        mTitle.initPageController(2,1);
        mTitle.setTitle(getString(R.string.titleMailInbox));

		
		//(WorkspaceView)findViewById(R.id.mailWorkspace);
		mailWorkspace.setScrollToScreenCallback(this);

		MailAdapter inboxAdapter = new MailAdapter(this);
		MailAdapter outboxAdapter = new MailAdapter(this);
		mInbox = new MailList(this, inboxAdapter, DoubanListType.MAIL_INBOX);
		mOutbox = new MailList(this, outboxAdapter, DoubanListType.MAIL_OUTBOX);
			
		mailWorkspace.addView(mInbox.getMainview());
		mailWorkspace.addView(mOutbox.getMainview());
		
		inboxAdapter.setList(mInbox);
		outboxAdapter.setList(mOutbox);

		mInbox.refresh();
		mPopup = new PopupRecommend(this);
		
	}

	//当在邮件详情下点击删除按钮！,startActivityForResult 在mailList中实现
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data){
		super.onActivityResult(requestCode, resultCode, data);
		if(Assistant.DEL_MAIL_SUCCESS == resultCode){
//			int index = data.getIntExtra("INDEX", -1);
			if(MailList.INBOX == requestCode){
				mInbox.removeOneMail(mInbox.getCurrentIndex());
			}else{
				mOutbox.removeOneMail(mOutbox.getCurrentIndex());
			}
			mPopup.showToastWindow(Assistant.SUCCESS_POP, "成功删除豆邮");
		}else if(Assistant.DEL_MAIL_UN_SUCCESS == resultCode){
			mPopup.showToastWindow(Assistant.FAIL_POP, "删除豆邮失败");
		}
	}
	
	
	public void callback(int index){
		mTitle.setCurrentPage(index+1);
		if(1 == index){
			mTitle.setTitle(getString(R.string.titleMailOutbox));
			if(mOutbox.getIsFirst()){
				mOutbox.refresh();
			}
		}else if(0 == index){
			mTitle.setTitle(getString(R.string.titleMailInbox));
		}
	}
	
	
    @Override
    public void onResume() {
        MobclickAgent.onResume(this);
        super.onResume();    //Super Method super.onResume();
    }

    @Override
    public void onPause() {
        MobclickAgent.onPause(this);
        super.onPause();    //Super Method super.onPause();
    }
}